﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Milestone.cs" company="Mar3ek">
//   http://mar3ek.wordpress.com
// </copyright>
// <summary>
//   Class representing a milestone
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace LighthouseNET
{
    using System;
    using System.Xml.Linq;

    /// <summary>
    /// Class representing a milestone
    /// </summary>
    public class Milestone
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Milestone"/> class.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="goals">The goals.</param>
        /// <param name="dueOn">The due on date.</param>
        public Milestone(string title, string goals, DateTime? dueOn)
        {
            Title = title;
            Goals = goals;
            DueOn = dueOn;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Milestone"/> class. 
        /// </summary>
        /// <param name="createdAt">
        /// The created at.
        /// </param>
        /// <param name="dueOn">
        /// The due on.
        /// </param>
        /// <param name="goals">
        /// The goals.
        /// </param>
        /// <param name="goalsHtml">
        /// The goals HTML.
        /// </param>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <param name="openTicketsCount">
        /// The open tickets count.
        /// </param>
        /// <param name="permalink">
        /// The permalink.
        /// </param>
        /// <param name="projectId">
        /// The project id.
        /// </param>
        /// <param name="ticketsCount">
        /// The tickets count.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        private Milestone(
            DateTime createdAt,
            DateTime dueOn,
            string goals,
            string goalsHtml,
            int id,
            int openTicketsCount,
            string permalink,
            int projectId,
            int ticketsCount,
            string title)
        {
            Title = title;
            Goals = goals;
            DueOn = dueOn;
            CreatedAt = createdAt;
            Goals = goals;
            GoalsHtml = goalsHtml;
            Id = id;
            OpenTicketsCount = openTicketsCount;
            Permalink = permalink;
            ProjectId = projectId;
            TicketsCount = ticketsCount;
        }

        #region Properties
        /// <summary>
        /// Gets the time of creation of this milestone.
        /// </summary>
        public DateTime CreatedAt { get; private set; }

        /// <summary>
        /// Gets the time this milestone is due on.
        /// </summary>
        public DateTime? DueOn { get; private set; }

        /// <summary>
        /// Gets the goals as HTML.
        /// </summary>
        public string GoalsHtml { get; private set; }

        /// <summary>
        /// Gets the goals.
        /// </summary>
        public string Goals { get; private set; }

        /// <summary>
        /// Gets the id.
        /// </summary>
        public int Id { get; private set; }

        /// <summary>
        /// Gets the open tickets count.
        /// </summary>
        public int OpenTicketsCount { get; private set; }

        /// <summary>
        /// Gets the permalink.
        /// </summary>
        public string Permalink { get; private set; }

        /// <summary>
        /// Gets the project id.
        /// </summary>
        public int ProjectId { get; private set; }

        /// <summary>
        /// Gets the tickets count.
        /// </summary>
        public int TicketsCount { get; private set; }

        /// <summary>
        /// Gets the title.
        /// </summary>
        public string Title { get; private set; } 
        #endregion

        /// <summary>
        /// Parses the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>Parsed milestone</returns>
        /// <exception cref="ArgumentNullException">Element is null or empty.</exception>
        internal static Milestone Parse(XElement element)
        {
            if (element == null || element.IsEmpty)
            {
                throw new ArgumentNullException("element");
            }

            // ReSharper disable PossibleNullReferenceException
            var createdAd = Globals.DateTimeFromLighthouse(element.Element("created-at").Value);

            var dueOn = Globals.DateTimeFromLighthouse(element.Element("due-on").Value);
            var goals = element.Element("goals").Value;
            var goalsHtml = element.Element("goals-html").Value;
            var id = int.Parse(element.Element("id").Value);
            int openTicketsCount;
            int.TryParse(element.Element("open-tickets-count").Value, out openTicketsCount);
            var permalink = element.Element("permalink").Value;
            var projectId = int.Parse(element.Element("project-id").Value);
            int ticketsCount;
            int.TryParse(element.Element("tickets-count").Value, out ticketsCount);
            var title = element.Element("title").Value;

            // ReSharper restore PossibleNullReferenceException
            return new Milestone(
                createdAd,
                dueOn,
                goals,
                goalsHtml,
                id,
                openTicketsCount,
                permalink,
                projectId,
                ticketsCount,
                title);
        }
    }
}
